qui(
    function (require, exports, module) {
        Ancestor.Auth = (function () {
            var url = url || Com.url.parse(window.location.href)['path'];
            var baseInfo = {
                url: url,
                authUrl: '/api/permission',
                loginUrl: 'http://lego.iqiyi.com/login?url=http://lego.iqiyi.com%2F',
                url403: 'http://lego.iqiyi.com/common/403.jsp',
                loginOut: 'http://lego.iqiyi.com/logout',
                navListUrl: 'http://ulive.lego.iqiyi.com/live-web/services/liveNav.do',
                authInfo: null,
                tips: 0 //0 304页面提示 1头部标签提示
            }
            var jump2TargetPage = function (url) {
                window.location.href = url;
            }
            var getDev = function () {
                return parseInt(Com.cookie.get('dev'));
            };

            function getVersion(){
                return "4.3.172";
            }

            function setWaring(tips) {
                var warning = '<div  style="display:block;position: fixed;top: 5px;left: 10%;width: 80%;z-index: 100000;text-align: center;height: 26px;line-height: 26px;color: red;background: #fff1d3;margin-bottom: 16px;text-shadow: 0 1px 0 rgba(255,255,255,0.5);"><strong>提示!</strong> ' + tips + '</div>';
                return warning;
            }

            // ajax 请求
            var getAuthData = function (options, callback) {
                options = options || {};
                var tempBaseInfo = $.extend(true, baseInfo, options);
                Ancestor.Dao.ajax({
                    url: tempBaseInfo.authUrl,
                    dataType: 'json',
                    method: 'get',
                    cache: false,
                    //async: false,
                    data: {'uri': tempBaseInfo.url.split('?')[0]},
                    success: function (data) {
                        //console.log(data);
                        if (options.tips) {
                            //   if (baseInfo.tips) {
                            if (data.code == 'A00304')
                                callback.call(this, 1);   // 无权限
                            else
                                callback.call(this, null); // 有权限
                            return;
                        }
                        switch (data.code) {
                            case 'A00000': // 已登陆
                                tempBaseInfo.authInfo = data.data;
                                var newDev = 0;
                                if (tempBaseInfo.authInfo.devMode) {
                                    newDev = 1
                                }
                                var dev = getDev();
                                showLogInfo( baseInfo );
                                callback.call(this, "suc")
                                if (dev == NaN || dev != newDev) {
                                    Com.cookie.set('dev', newDev);
                                    window.location.reload();
                                }
                                break;
                            case 'A00303': // 未登陆
                            case 'A00401':
                                jump2TargetPage(tempBaseInfo.loginUrl);
                                break;

                            case 'A00304': // 无权限
                                //jump2TargetPage(baseInfo.url403);
                                var warning = setWaring('sorry,您没有操作本页面的权限！');
                                $('body').html('');
                                $('body').html(warning);
                                break;
                        }
                    },
                    error: function (data) {
                        var warning = setWaring('网络故障,<a href="javascript:void(0);" onclick = "window.location.reload();">请重刷！</a>');
                        $('body').html(warning);
                    }
                }, "/permission")

            }

            function queryNavList(baseInfo){
                var request = {};
                request.url = baseInfo.navListUrl;
                request.type = 'get';
                request.params = {};
                request.needCookie = true;
                request = JSON.stringify(request);
                Ancestor.Dao.redirectRequest({request: request} , function(data){
                    if(!data) {
                        console.warn(" 获取导航条失败!")
                        return;
                    }
                    var name, items, itemName, itemUrl;
                    var navListData = [];
                    for(var nav in data){
                        name = data[nav]['name'];
                        navListData.push('<dl class="panel-live_demand clearfix"><dt><strong>' + name + '</strong></dt><dd>');
                        items = data[nav]['items'];
                        for(var item in items){
                            itemName = items[item]['name'];
                            itemUrl = items[item]['url'];
                            navListData.push('<a href="' + itemUrl + '">' + itemName +'</a>')
                        }
                        navListData.push('</dd></dl>')
                    }
                    $('#panel-nav').html(navListData.join(''));
                } )
            }

            function showLogInfo(baseInfo) {
                var header = [
                    '<div class="top">',
                    '<div class="logo">',
                    '<a href="http://lego.iqiyi.com/" alt="乐高首页"></a>',
                    '</div>',
                    '<div class="user">',
                        '<input type="hidden" name="hidden_uid" value="' + baseInfo.authInfo.uid + '">',
                        ' Hi, <a href="" class="name">' + baseInfo.authInfo.userName + '</a>&nbsp;',
                    '[<a href="http://static.lego.iqiyi.com/help/lego-help-v1.docx" class="help">帮助</a>]&nbsp;',
                        '[<a href="' + baseInfo.loginOut + '" class="help">退出</a>]',
                    '</div>',
                    '</div>',
                    '<div class="nav">',
                    '<ol>',
                    '<li class="channel-index "><a href="http://ulive.lego.iqiyi.com/live-web/home.do" class="">我的首页</a><i></i></li>',
                    '<li class="channel-catalog selected"><a href="javascript:void(0)" class="selected">直播中心</a><i></i>',
                    '<div class="panel" id="panel-nav">',
                    '</div>',
                    '</li>',
                    '</ol>',
                    '</div>'
                ].join('');
                var epg_version = auth.version() + (auth.dev() ? "_test" : "_production");
                $("header").html(header);
                $('body').append('<div class="scroll-to-top scroll-to-top-hide" id="BackToTopButton"><a class="btn" href="#">返回顶部</a></div><footer>版本 ' + epg_version + ' | Copyright ©2017 <a href="http://www.iqiyi.com" target="_blank">iQIYI.com</a></footer>');
                $(window).bind('scroll', function () {
                    Com.page.scrollToTop($("#BackToTopButton"));
                });
                queryNavList(baseInfo)
            }

            return{
                getAuthData: getAuthData,
                baseInfo: baseInfo,
                jump2TargetPage: jump2TargetPage,
                dev: getDev, //开发环境
                version:getVersion
            };
        })();
        var auth = Ancestor.Auth;
        var dev = auth.dev();
        window.auth = auth;
        window.dev = dev;
        console.log(" window.auth done");
    }
);